package com.example.tts.controller;

import marytts.LocalMaryInterface;
import marytts.MaryInterface;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import java.io.*;

@Controller
public class TTSController {

    @RequestMapping("/")
    public String index() {
        return "tts.html";
    }

    @RequestMapping("/process")
    public void process(HttpServletRequest request, HttpServletResponse response) throws Exception {

        MaryInterface mary = new LocalMaryInterface();
//        mary.setAudioEffects("F0Scale(f0Scale:0.0)+Robot(amount:75.0)+Whisper(amount:35)");
//        mary.setAudioEffects("Robot(amount:100)+Stadium(amount:200)");
        mary.setAudioEffects("");
//        mary.setAudioEffects("Volume(amount:2.0)+TractScaler(amount:1.5)+F0Scale(f0Scale:2.0)+F0Add(f0Add:50.0)+Rate(durScale:1.5)+Robot(amount:100.0)+Whisper(amount:100.0)+Stadium(amount:100.0)+Chorus(delay1:466;amp1:0.54;delay2:600;amp2:-0.10;delay3:250;amp3:0.30)+FIRFilter(type:3;fc1:500.0;fc2:2000.0)");
        String text = "Easily convert your US English text into professional speech for free. Perfect for e-learning, presentations, YouTube videos and increasing the accessibility of your website. Our voices pronounce your texts in their own language using a specific accent. Plus, these texts can be downloaded as MP3. In some languages, multiple speakers are available.";
        AudioInputStream audioInputStream = mary.generateAudio(text);
        //方式一
//        File file = new File("D://1.mp3");
//        FileInputStream fis = new FileInputStream(file);
//        ByteArrayOutputStream os = new ByteArrayOutputStream();

//        HttpHeaders header = new HttpHeaders();
//        header.setContentType(new MediaType("audio","x-wav"));
////        header.setContentLength(audioInputStream.getFrameLength());
//        IOUtils.copy(fis, os);
//        os.flush();
//        os.close();
//
//        return new HttpEntity<byte[]>(os.toByteArray(), header);
        File file = new File("D://1.mp3");
        OutputStream os = response.getOutputStream();
        FileInputStream fis = new FileInputStream(file);
        //方式二
        response.setHeader("Content-Type", "audio/x-wav");
//        response.setContentType("audio/x-wav");
        response.setHeader("Content-Length", String.valueOf(audioInputStream.getFrameLength()));
//        byte[] buf = new byte[2048];
//        int len_l = 0;
//        while ((len_l = fis.read(buf)) != -1) {
//            os.write(buf, 0, len_l);
//
//        }
//        fis.close();
//        os.flush();
//        os.close();
        AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, os);
        os.flush();
        os.close();
        response.flushBuffer();
    }
}
